#include "tableWidget.h"

tableWidget::tableWidget()
{
	//控件初始化
	addRowBtn = new QPushButton("增加行");
	addColBtn = new QPushButton("增加列");
	removeRowBtn = new QPushButton("删除行");
	removeColBtn = new QPushButton("删除列");
	table = new QTableWidget(2,3);
	//布局
	QVBoxLayout *vLay = new QVBoxLayout();
	vLay->addWidget(addRowBtn);
	vLay->addWidget(addColBtn);
	vLay->addWidget(removeRowBtn);
	vLay->addWidget(removeColBtn);
	QHBoxLayout *hLay = new QHBoxLayout();
	hLay->addWidget(table);
	hLay->addLayout(vLay);
	setLayout(hLay);
	//绑定信号和槽函数
	connect(addRowBtn, SIGNAL(clicked()), this, SLOT(addRow()));
	connect(addColBtn, SIGNAL(clicked()), this, SLOT(addCol()));
	connect(removeRowBtn, SIGNAL(clicked()), this, SLOT(removeRow()));
	connect(removeColBtn, SIGNAL(clicked()), this, SLOT(removeCol()));
	//设置表头
	QStringList head;
	head<<"姓名"<<"年龄"<<"性别";
	table->setHorizontalHeaderLabels(head);
	//设置单元格
	table->setItem(0, 0, new QTableWidgetItem("魏杰"));
	table->setItem(0, 1, new QTableWidgetItem("30"));
	QComboBox *com1 = new QComboBox();
	QComboBox *com2 = new QComboBox();
	QStringList sex;
	sex<<"男"<<"女";
	com1->addItems(sex);
	com2->addItems(sex);
	table->setCellWidget(0, 2, com1);
	table->setCellWidget(1, 2, com2);
	
	resize(800, 800);
	setWindowTitle("hehe");
	
}

void tableWidget::addRow()
{
	int num = table->rowCount();
	table->insertRow(num);
}
void tableWidget::addCol()
{
	int num = table->columnCount();
	table->insertColumn(num);
}
void tableWidget::removeRow()
{
	int num = table->rowCount();
	table->removeRow(num-1);
}
void tableWidget::removeCol()
{
	int num = table->columnCount();
	table->removeColumn(num-1);

}
